﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GlxRRSDB.Data;
using GlxRRSDB.Utils;
using GlxRRSReporter.Utils;
using Excel = Microsoft.Office.Interop.Excel;
using GlxRRSDB;

namespace GlxRRSReporter.DailyReporter
{
    public class TicketCinemaReporter
    {
        private const string SHEET_PER_CINEMA = "Per Cinema";
        private const string SHEET_1 = "Sheet1";
        private const string SHEET_3 = "Sheet3";

        private static string TEMPLATE_MONTHLY_NAME = ".\\templates\\TicketReportByCinemaTemplate.xls";
        private static string OUT_MONTHLY_NAME = ".\\templates\\export\\TicketReportByCinema";
        public static void init(string template, string outNameVal)
        {
            TEMPLATE_MONTHLY_NAME = template;
            OUT_MONTHLY_NAME = outNameVal;
        }
        public static void generateTicketReportByCinema(DateTime date, int cinemaId, string cinemaName,
            List<ReportDataSet.tblTicketRevenueRow> dictTicket,
            List<ReportDataSet.tblCORevenueRow> listCORevenue,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailAdmissionFilm,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailBOFilm
            )
        {
            // load excel file
            Excel.Workbook workbook = ExportToExcel.openExcelWorkbooks(TEMPLATE_MONTHLY_NAME);
            //
            Excel.Worksheet sheet;

            #region SHEET Per Cinema
            sheet = (Excel.Worksheet)workbook.Worksheets[SHEET_PER_CINEMA];
            fillReportData(sheet, date, cinemaId, cinemaName, dictTicket, listCORevenue);
            #endregion

            #region SHEET Sheet 1
            sheet = (Excel.Worksheet)workbook.Worksheets[SHEET_1];
            fillChartData(sheet, date, dictDetailAdmissionFilm, dictDetailBOFilm);
            #endregion
            // save
            workbook.SaveAs(OUT_MONTHLY_NAME);
        }

        private static void fillReportData(Excel.Worksheet sheet, DateTime date, int cinemaId, string cinemaName,
            List<ReportDataSet.tblTicketRevenueRow> dictTicket,
            List<ReportDataSet.tblCORevenueRow> listCORevenue
        )
        {
            DateTime startOfWeek = date.StartOfWeek(DayOfWeek.Friday);
            sheet.get_Range("K2").Formula = "Galaxy " + cinemaName;
            sheet.get_Range("B8").Formula = startOfWeek;//.ToShortDateString();
            //
            string[] columnSheet1 = new string[] { "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N", "P", "R" };
            string[] rowSheet1 = new string[] { "8", "9", "10", "11", "12", "13", "14" };
            //
            int rowIdx = -1;
            int columnIdx = -1;
            string ad;
            int initTransCORowIdx = columnSheet1.Count() - 2;
            Dictionary<DateTime, COTrans> dictCOTrans = generateTODictCOTrans(listCORevenue);
            foreach (COTrans coTrans in dictCOTrans.Values)
            {
                columnIdx = initTransCORowIdx;
                TimeSpan ts = coTrans.dt.Date.Subtract(startOfWeek);
                rowIdx = ts.Days;
                //
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = coTrans.trans;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = coTrans.CORevenue;
            }
            foreach (ReportDataSet.tblTicketRevenueRow dict in dictTicket)
            {
                columnIdx = -1;
                TimeSpan ts = dict.Date.Date.Subtract(startOfWeek);
                rowIdx = ts.Days;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.Adult2D;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.Child2D;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.Senior2D;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.Student2D;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.CompTotal2D;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.HappyDay2D;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.Revenue2D;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.Standard3D;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.VIP3D;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.HappyDay3D;
                //
                columnIdx++;
                ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                sheet.get_Range(ad).Formula = dict.Revenue3D;
                //
                //columnIdx++;
                //ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                //sheet.get_Range(ad).Formula = dict.Trans;
                ////
                //columnIdx++;
                //ad = columnSheet1[columnIdx] + rowSheet1[rowIdx];
                //sheet.get_Range(ad).Formula = dict.RevenueConcession;
            }
        }

        private static void fillChartData(Excel.Worksheet sheet, DateTime date,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailAdmissionFilm,
            Dictionary<int, Dictionary<DateTime, DailyReportData>> dictDetailBOFilm)
        {
            DateTime startOfWeek = date.StartOfWeek(DayOfWeek.Friday);
            string[] column = new string[] { "B", "C", "D", "E", "F", "G", "H", "I" };
            string[] rowAdmissionFilm = new string[] { "6", "7", "8", "9", "10", "11", "12", "13" };
            string[] rowBOFilm = new string[] { "20", "21", "22", "23", "24", "25", "26", "27" };
            // title
            sheet.get_Range("C5").Formula = startOfWeek;//.Date.ToString();
            sheet.get_Range("C19").Formula = startOfWeek;//.Date.ToString();
            #region CHART Film Admission DETAIL
            int columnIdx = -1;
            int rowIdx = -1;
            foreach (Dictionary<DateTime, DailyReportData> dict in dictDetailAdmissionFilm.Values)
            {
                columnIdx = 0;
                rowIdx++;
                foreach (DailyReportData detail in dict.Values)
                {
                    System.TimeSpan diff1 = detail.FromDate.Date.Subtract(startOfWeek.Date);
                    columnIdx = diff1.Days + 1;
                    //
                    sheet.get_Range(column[0] + rowAdmissionFilm[rowIdx]).Formula = detail.FilmTitle;
                    string addr = column[columnIdx] + rowAdmissionFilm[rowIdx];
                    sheet.get_Range(addr).Formula = (int)detail.currentDate;
                }
            }
            #endregion
            #region CHART Film BO DETAIL
            rowIdx = -1;
            foreach (Dictionary<DateTime, DailyReportData> dict in dictDetailBOFilm.Values)
            {
                rowIdx++;
                foreach (DailyReportData detail in dict.Values)
                {
                    System.TimeSpan diff1 = detail.FromDate.Date.Subtract(startOfWeek.Date);
                    columnIdx = diff1.Days + 1;
                    //
                    sheet.get_Range(column[0] + rowBOFilm[rowIdx]).Formula = detail.FilmTitle;
                    //
                    string addr = column[columnIdx] + rowBOFilm[rowIdx];
                    sheet.get_Range(addr).Formula = detail.currentDate;
                }
            }
            #endregion
        }
        private static Dictionary<DateTime, COTrans> generateTODictCOTrans(List<ReportDataSet.tblCORevenueRow> listCORevenue)
        {
            Dictionary<DateTime, COTrans> result = new Dictionary<DateTime, COTrans>();
            foreach (ReportDataSet.tblCORevenueRow co in listCORevenue)
            {
                if (result.ContainsKey(co.Date))
                {
                    COTrans trans = result[co.Date];
                    trans.trans++;
                    trans.CORevenue += co.CO;
                }
                else
                {
                    COTrans trans = new COTrans();
                    trans.dt = co.Date;
                    trans.trans++;
                    trans.CORevenue += co.CO;
                    result.Add(trans.dt, trans);
                }
            }
            return result;
        }
        private class COTrans
        {
            public DateTime dt = DateTime.MinValue;
            public int trans = 0;
            public double CORevenue = 0;
        }
    }
}
